<template>
  <view class="order-closure-page page-content">
    <z-paging ref="paging" v-model="currentList" @query="queryList" :auto="false" :refresher-enabled="true"
      :loading-more-enabled="true" empty-view-text="暂无数据">
      <template #top>
        <up-navbar title="封顶超额列表" :autoBack="false" :fixed="true" bgColor="transparent" :border="false" placeholder
          @leftClick="handleBackClick">
          <template #right>
            <view class="right-btn-item" @click="isCheck = !isCheck">
              <text>{{ isCheck ? '取消' : '批量转移' }}</text>
            </view>
          </template>
        </up-navbar>

        <up-sticky class="padding-20" bgColor="transparent">
          <view class="header-bar">
            <view class="header-bar-location" @click="showTimePicker = true">
              <text class="title">时间</text>
              <up-icon name="arrow-down"></up-icon>
            </view>
            <view class="time-picker">
              <text class="title"></text>
              <uni-datetime-picker type="daterange" :border="false"></uni-datetime-picker>
            </view>
            <up-picker :show="showTimePicker" :columns="columns" :closeOnClickOverlay="true"
              @confirm="showTimePicker = false" @cancel="showTimePicker = false"
              @close="showTimePicker = false"></up-picker>
          </view>

          <up-search :clearabled="true" :showAction="false" placeholder="请输入车牌号"></up-search>
          <up-tabs :list="tabsList" :scrollable="false" @change="handleTabChange"></up-tabs>
        </up-sticky>
      </template>

      <view class="order-closure-list">
        <view class="list-item-card" v-for="(item, index) in currentList" :key="index">
          <view class="card-header">
            <view class="title">
              订单号：<text class="number">516384358</text>
            </view>
          </view>
          <view class="card-content-box">
            <view class="content">
              <view class="item">
                <text class="label">
                  状态
                </text>
                <view class="value">
                  <view class="item-tag">
                    <up-tag text="待审核" type="warning"></up-tag>
                  </view>
                </view>
              </view>
              <view class="item">
                <text class="label">
                  车牌号：
                </text>
                <text class="value">
                  粤J13545
                </text>
              </view>
              <view class="item">
                <text class="label">
                  异常类型：
                </text>
                <text class="value">
                  设备异常
                </text>
              </view>
              <view class="item">
                <text class="label">
                  异常时间：
                </text>
                <text class="value">
                  2025-9-1 11:02:35
                </text>
              </view>
              <!-- 停车位置 -->
              <view class="item">
                <text class="label">
                  停车位置：
                </text>
                <text class="value">
                  顺德
                </text>
              </view>
              <view class="item">
                <text class="label">
                  驶入时间：
                </text>
                <text class="value">
                  2025-9-1 11:02:35
                </text>
              </view>
            </view>
            <view class="card-footer">
              <view class="checkbox-box">
                <up-checkbox name="item.name" v-if="isCheck"> </up-checkbox>
              </view>

              <view class="operate">
                <up-button type="primary" text="重新计费" plain class="custom-style"
                  @click="handleRecharge(item)"></up-button>
                <up-button type="primary" text="处理异常" class="custom-style" @click="handleGo(item)"></up-button>
              </view>
            </view>
          </view>
        </view>
      </view>
    </z-paging>

    <OrderAbnormalModal v-model:show="show" :type="type" :row-data="editingData" />

    <view class="bottom-bar" v-if="isCheck">
      <view class="select-all1">
        <view>
          <up-checkbox-group v-model="checkboxValue" placement="column" shape="circle" activeColor="#FE6000">
            <up-checkbox label="全选" name="全选">
            </up-checkbox>
          </up-checkbox-group>
        </view>
        <view class="selected-count">合计：<text class="red">5</text></view>
      </view>

      <view class="confirm-btn" @click="handleRecharge">重新计费</view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { getRealNameVerifyList } from '@/api';
import OrderAbnormalModal from './components/OrderAppealModal.vue'

const { bool: show, setTrue: openModal } = useBoolean();
const type = ref('add')
const editingData = ref({})

const isCheck = ref(false);
const checkboxValue = ref([]);

const showTimePicker = ref(false);
const columns = reactive([
  ['近7天', '近1个月', '近3个月', '近6个月', '近1年'],
]);

// 选项卡数据  
const tabsList = [
  { name: '全部' },
  { name: '待处理' },
  { name: '已处理' },
  { name: '我的处理' },
];
const currentTab = ref(0);

// z-paging 组件引用
const paging = ref(null)

// 当前显示的列表数据
const currentList = ref([])

const handleRecharge = (item) => {
  uni.showModal({
    title: '重新计费',
    content: '确定重新计费吗？',
    success: (res) => {
      if (res.confirm) {
        console.log('用户点击了确定')
        isCheck.value = false
      } else if (res.cancel) {
        console.log('用户点击了取消')
        isCheck.value = false
      }
    }
  })
}

const handleGo = (item) => {
  type.value = 'edit'
  editingData.value = {
    status: '待处理',
    orderId: 10000,
  }
  openModal()
}

// 选项卡切换事件
const handleTabChange = (e) => {
  const newIndex = e.index;
  if (currentTab.value === newIndex) {
    return; // 如果是同一个tab，不需要重新加载
  }

  currentTab.value = newIndex;

  // 切换tab时重新加载数据
  if (paging.value) {
    paging.value.reload();
  }
};

// 处理返回点击事件
const handleBackClick = () => {
  uni.redirectTo({
    url: '/pages/home/index'
  })
}


// 获取车辆列表数据
const queryList = async (pageNo, pageSize) => {
  console.log(pageNo, pageSize, currentTab.value)

  const params = {
    pageNum: pageNo,
    pageSize
  }

  await fetchList(params)
};

const fetchList = async (params) => {
  try {
    const result: any = await getRealNameVerifyList({ ...params, status: currentTab.value === 0 ? undefined : currentTab.value })
    const data = result.data
    paging.value.complete(data.records || [])
  } catch (error) {
    paging.value.complete(false)
  }
}


// 搜索按钮点击事件
const handleSearch = () => {
};

// 查看详情
const handleView = (row: any) => {
  // 跳转到详情页逻辑
  console.log('查看详情:', row);
};

// 审核操作
const handleVerify = (row: any) => {
  // 跳转到审核页逻辑
  console.log('审核操作:', row);
};

// 页面挂载时获取数据
onMounted(() => {
  fetchList({
    pageNum: 1,
    pageSize: 10,
  })
});
</script>

<style scoped lang="scss">
.order-closure-list {
  padding: 20px;
}

.bottom-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 120rpx;
  background-color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30rpx;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);

  .select-all1 {
    display: flex;
    align-items: center;

    .selected-count {
      font-size: 24rpx;
      color: #999;
      margin-left: 30rpx;

      .red {
        color: #ff5c5c;
        font-weight: 500;
      }
    }
  }

  .confirm-btn {
    width: 260rpx;
    height: 72rpx;
    background: linear-gradient(180deg, #ff9900 0%, #fe6000 100%);
    border-radius: 172rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    font-size: 32rpx;
    font-weight: 500;
  }
}

.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0rpx 20rpx 30rpx;

  .time-picker {
    width: 480rpx;
    display: flex;
    align-items: center;

    .title {
      font-size: 28rpx;
      font-weight: bold;
      color: #333;
      margin-right: 10rpx;
    }
  }

  &-location {
    display: flex;
    align-items: center;

    .location-icon {
      width: 40rpx;
      height: 40rpx;
      margin-right: 10rpx;
    }

    .title {
      font-size: 28rpx;
      font-weight: bold;
      color: #333;
      margin-right: 10rpx;
    }

    .arrow-icon {
      width: 30rpx;
      height: 30rpx;
    }
  }

  .waiting-count {
    font-weight: 500;
    font-size: 24rpx;
    color: #333333;
    display: flex;
    align-items: center;

    text {
      font-weight: bold;
      font-size: 36rpx;
      color: #fe6000;
      margin-left: 10rpx;
    }
  }
}

/* 列表统计 */
.list-statistics {
  display: flex;
  flex-wrap: wrap;
  padding: 20rpx 0;
}

.list-statistics .item {
  flex: 1 1 20%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
}

.list-statistics .label {
  font-size: 14px;
  color: #666;
}

.list-statistics .value {
  font-size: 18px;
  font-weight: bold;
  color: #e9710f;
}
</style>